//
//  BankView.m
//  CocoaSynth
//
//  Created by Thomas Dupont on 19/02/10.
//  Copyright 2010 Apple Inc. All rights reserved.
//

#import "BankView.h"

#import "BankModuleViewControllerProtocol.h";

#define myPrivateType @"myPrivateType"

@implementation BankView

@synthesize controller;
@synthesize moduleImage;

- (NSDragOperation)draggingSourceOperationMaskForLocal:(BOOL)local
{
	if (!local) {
		return NSDragOperationNone;
	} else {
		return NSDragOperationCopy;
	}
}

- (void)mouseDown:(NSEvent *)event
{
	[event retain];
	[mouseDownEvent autorelease];
	mouseDownEvent = event;
}

- (void)mouseDragged:(NSEvent *)event
{
	NSPoint down = [mouseDownEvent locationInWindow];
	NSPoint drag = [event locationInWindow];
	float distance = hypot(down.x - drag.x, down.y - drag.y);
	if (distance < 3) {
		return;
	}
	
	NSPoint point = NSMakePoint(self.bounds.origin.x, self.bounds.origin.y);
	
	NSPasteboard* pb = [NSPasteboard pasteboardWithName:NSDragPboard];
	
	NSData *data = [NSKeyedArchiver archivedDataWithRootObject:controller];
    [pb declareTypes:[NSArray arrayWithObject:myPrivateType] owner:self];
    [pb setData:data forType:myPrivateType];
	
	
	[self dragImage:moduleImage
				 at:point
			 offset:NSMakeSize(0, 0)
			  event:mouseDownEvent
		 pasteboard:pb
			 source:self
		  slideBack:YES];
}

@end
